3 * Formats credits for articles
5 * Copyright 2004, Evan Prodromou <evan@wikitravel.org>.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 * @author <evan@wikitravel.org>
26 use MediaWiki\MediaWikiServices
;
31 class CreditsAction
extends FormlessAction
{
33 public function getName() {
37 protected function getDescription() {
38 return $this->msg( 'creditspage' )->escaped();
42 * This is largely cadged from PageHistory::history
46 public function onView() {
47 if ( $this->page
->getID() == 0 ) {
48 $s = $this->msg( 'nocredits' )->parse();
50 $s = $this->getCredits( -1 );
53 return Html
::rawElement( 'div', [ 'id' => 'mw-credits' ], $s );
57 * Get a list of contributors
59 * @param int $cnt Maximum list of contributors to show
60 * @param bool $showIfMax Whether to contributors if there more than $cnt
63 public function getCredits( $cnt, $showIfMax = true ) {
67 $s = $this->getAuthor( $this->page
);
68 if ( $cnt > 1 ||
$cnt < 0 ) {
69 $s .= ' ' . $this->getContributors( $cnt - 1, $showIfMax );
77 * Get the last author with the last modification time
81 protected function getAuthor( Page
$page ) {
82 $user = User
::newFromName( $page->getUserText(), false );
84 $timestamp = $page->getTimestamp();
86 $lang = $this->getLanguage();
87 $d = $lang->date( $page->getTimestamp(), true );
88 $t = $lang->time( $page->getTimestamp(), true );
94 return $this->msg( 'lastmodifiedatby', $d, $t )->rawParams(
95 $this->userLink( $user ) )->params( $user->getName() )->escaped();
99 * Whether we can display the user's real name (not a hidden pref)
104 protected function canShowRealUserName() {
105 $hiddenPrefs = $this->context
->getConfig()->get( 'HiddenPrefs' );
106 return !in_array( 'realname', $hiddenPrefs );
110 * Get a list of contributors of $article
111 * @param int $cnt Maximum list of contributors to show
112 * @param bool $showIfMax Whether to contributors if there more than $cnt
113 * @return string Html
115 protected function getContributors( $cnt, $showIfMax ) {
116 $contributors = $this->page
->getContributors();
118 $others_link = false;
120 # Hmm... too many to fit!
121 if ( $cnt > 0 && $contributors->count() > $cnt ) {
122 $others_link = $this->othersLink();
124 return $this->msg( 'othercontribs' )->rawParams(
125 $others_link )->params( $contributors->count() )->escaped();
133 # Sift for real versus user names
134 /** @var User $user */
135 foreach ( $contributors as $user ) {
137 if ( $user->isLoggedIn() ) {
138 $link = $this->link( $user );
139 if ( $this->canShowRealUserName() && $user->getRealName() ) {
140 $real_names[] = $link;
142 $user_names[] = $link;
145 $anon_ips[] = $this->link( $user );
153 $lang = $this->getLanguage();
155 if ( count( $real_names ) ) {
156 $real = $lang->listToText( $real_names );
161 # "ThisSite user(s) A, B and C"
162 if ( count( $user_names ) ) {
163 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
164 count( $user_names ) )->escaped();
169 if ( count( $anon_ips ) ) {
170 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
171 count( $anon_ips ) )->escaped();
176 # This is the big list, all mooshed together. We sift for blank strings
178 foreach ( [ $real, $user, $anon, $others_link ] as $s ) {
179 if ( $s !== false ) {
180 array_push( $fulllist, $s );
184 $count = count( $fulllist );
186 # "Based on work by ..."
188 ?
$this->msg( 'othercontribs' )->rawParams(
189 $lang->listToText( $fulllist ) )->params( $count )->escaped()
194 * Get a link to $user's user page
196 * @return string Html
198 protected function link( User
$user ) {
199 if ( $this->canShowRealUserName() && !$user->isAnon() ) {
200 $real = $user->getRealName();
202 $real = $user->getName();
205 $page = $user->isAnon()
206 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
207 : $user->getUserPage();
209 return MediaWikiServices
::getInstance()
210 ->getLinkRenderer()->makeLink( $page, $real );
214 * Get a link to $user's user page
216 * @return string Html
218 protected function userLink( User
$user ) {
219 $link = $this->link( $user );
220 if ( $user->isAnon() ) {
221 return $this->msg( 'anonuser' )->rawParams( $link )->parse();
223 if ( $this->canShowRealUserName() && $user->getRealName() ) {
226 return $this->msg( 'siteuser' )->rawParams( $link )->params( $user->getName() )->escaped();
232 * Get a link to action=credits of $article page
233 * @return string HTML link
235 protected function othersLink() {
236 return MediaWikiServices
::getInstance()->getLinkRenderer()->makeKnownLink(
238 $this->msg( 'others' )->text(),
240 [ 'action' => 'credits' ]